package kr.co.dongpang.android;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.squareup.picasso.Picasso;
import com.sys.app.cms.IdPassword;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;

public class SmGoodEtcCustomDialog extends Activity {

	String goodcd;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		goodcd = getIntent().getStringExtra("goodcd");

		setContentView(R.layout.custom_dialog_activity);

		new detailTask().execute(null, null, null);
	}

	class detailTask extends AsyncTask<Void, Void, Void> {

		String shopname;
		String position;
		String tel;
		String madminid;
		String catcd;
		String shortdesc;
		String tmpgoodcd;

		@Override
		protected Void doInBackground(Void... params) {
			// TODO Auto-generated method stub

			JSONObject json = JSONfunctions.getJSONfromURL("http://dongpang.co.kr/android/dongpang.v01/sm_detail_etc.php?goodcd=" + goodcd + "&memberLevel=" + IdPassword.getLevel(SmGoodEtcCustomDialog.this));

			try {
				JSONArray dongpang = json.getJSONArray("dongpang");
				JSONObject jobject = dongpang.getJSONObject(0);
				shopname = jobject.getString("shopname");
				position = jobject.getString("position");
				tel = jobject.getString("tel");
				madminid = jobject.getString("madminid");
				catcd = jobject.getString("catcd");
				shortdesc = jobject.getString("shortdesc");
				tmpgoodcd = jobject.getString("tmpgoodcd");

			} catch (JSONException e) {
				Log.e("log_tag", "Error Parsing data" + e.toString());
			} 

			return null;
		}

		protected void onPostExecute(Void result) {

			((TextView) findViewById(R.id.shopname)).setText(shopname);
			((TextView) findViewById(R.id.position)).setText(position);
			((TextView) findViewById(R.id.tel)).setText(tel);
			((TextView) findViewById(R.id.message)).setText(shortdesc);
			
			((Button) findViewById(R.id.goShop)).setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					
					if(IdPassword.getDs(SmGoodEtcCustomDialog.this).equals("1")){
						Intent intent = new Intent(SmGoodEtcCustomDialog.this, SmShop.class);
						intent.putExtra("refcd", catcd);
						intent.putExtra("catnm", shopname);
						startActivity(intent);
					}
					
				}
			});
			
			String imgXS = "http://www.dongpang.co.kr/images/mAdmin/" + madminid + ".jpg";
			Picasso.with(SmGoodEtcCustomDialog.this).load(imgXS).into((ImageView) findViewById(R.id.img)); 
			
			Log.d("","tmpgoodcd:"+tmpgoodcd);
			
			if(!tmpgoodcd.equals("")){ 

				String[] detail = tmpgoodcd.split(":");
	
				if (detail[0].equals("1"))
					{ ((ImageView) findViewById(R.id.d1_1)).setBackgroundResource(R.drawable.icon_check_on); ((TextView) findViewById(R.id.t1_1)).setTextColor(Color.parseColor("#64bf3a")); }
				if (detail[0].equals("2"))
					{ ((ImageView) findViewById(R.id.d1_2)).setBackgroundResource(R.drawable.icon_check_on); ((TextView) findViewById(R.id.t1_2)).setTextColor(Color.parseColor("#64bf3a")); }
				if (detail[0].equals("3"))
					{ ((ImageView) findViewById(R.id.d1_3)).setBackgroundResource(R.drawable.icon_check_on); ((TextView) findViewById(R.id.t1_3)).setTextColor(Color.parseColor("#64bf3a")); }
	
				if (detail[1].equals("1"))
					{ ((ImageView) findViewById(R.id.d2_1)).setBackgroundResource(R.drawable.icon_check_on); ((TextView) findViewById(R.id.t2_1)).setTextColor(Color.parseColor("#64bf3a")); }
				if (detail[1].equals("2"))
					{ ((ImageView) findViewById(R.id.d2_2)).setBackgroundResource(R.drawable.icon_check_on); ((TextView) findViewById(R.id.t2_2)).setTextColor(Color.parseColor("#64bf3a")); }
				if (detail[1].equals("3"))
					{ ((ImageView) findViewById(R.id.d2_3)).setBackgroundResource(R.drawable.icon_check_on); ((TextView) findViewById(R.id.t2_3)).setTextColor(Color.parseColor("#64bf3a")); }
	
				if (detail[2].equals("1"))
					{ ((ImageView) findViewById(R.id.d3_1)).setBackgroundResource(R.drawable.icon_check_on); ((TextView) findViewById(R.id.t3_1)).setTextColor(Color.parseColor("#64bf3a")); }
				if (detail[2].equals("2"))
					{ ((ImageView) findViewById(R.id.d3_2)).setBackgroundResource(R.drawable.icon_check_on); ((TextView) findViewById(R.id.t3_2)).setTextColor(Color.parseColor("#64bf3a")); }
				if (detail[2].equals("3"))
					{ ((ImageView) findViewById(R.id.d3_3)).setBackgroundResource(R.drawable.icon_check_on); ((TextView) findViewById(R.id.t3_3)).setTextColor(Color.parseColor("#64bf3a")); }
	
				if (detail[3].equals("1"))
					{ ((ImageView) findViewById(R.id.d4_1)).setBackgroundResource(R.drawable.icon_check_on); ((TextView) findViewById(R.id.t4_1)).setTextColor(Color.parseColor("#64bf3a")); }
				if (detail[3].equals("2"))
					{ ((ImageView) findViewById(R.id.d4_2)).setBackgroundResource(R.drawable.icon_check_on); ((TextView) findViewById(R.id.t4_2)).setTextColor(Color.parseColor("#64bf3a")); }
				if (detail[3].equals("3"))
					{ ((ImageView) findViewById(R.id.d4_3)).setBackgroundResource(R.drawable.icon_check_on); ((TextView) findViewById(R.id.t4_3)).setTextColor(Color.parseColor("#64bf3a")); }

			}
			
			super.onPostExecute(result);

		}

	}
}
